package work.xiaohong.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.mybatisplus.entity.First;
import work.xiaohong.mybatisplus.service.IFirstService;
import work.xiaohong.utils.Result;
import work.xiaohong.utils.StatusCode;

import java.util.List;

/**
 *  前端控制器
 * @author xiaohong
 * @since 2021-10-24 10:35:40
 */
@Api(tags = "First接口文档")
@RestController
@RequestMapping("/first")
public class FirstController {

   @Autowired
   private IFirstService firstService;


    /***
    * 条件分页查询
    * @param first
    * @param page
    * @param size
    * @return
    */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "First对象",required = true) First first, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        Page<First> pageInfo = firstService.page(new Page<First>(page,size),new QueryWrapper<First>(first));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }


    /***
    * 分页查询
    * @param page:当前页
    * @param size:每页显示多少条
    * @return
    */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用FirstService实现分页查询First
        Page<First> pageInfo = firstService.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
    * 条件查询
    * @param first
    * @return
    */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<First>> findList(@RequestBody(required = false) @Parameter(name = "First对象",required = true) First first){
        List<First> list = firstService.list(new QueryWrapper<First>(first));
        return new Result<List<First>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }


    /***
    * 根据Id删除first数据
    * @param id
    * @return
    */
    @Operation(summary = "根据ID删除First",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value="id") Integer id){
        firstService.removeById(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }



    /***
    * 根据Id批量删除first数据
    * @param ids
    * @return
    */
    @Operation(summary = "根据Id批量删除First",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的First对象id集合",required = true) List<Integer> ids){
        firstService.removeByIds(ids);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
    * 根据ID修改First数据
    * @param first
    * @return
    */
    @Operation(summary = "根据ID修改First",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "First对象",required = true) First first){
        firstService.updateById(first);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }


     /***
     * 新增First数据
     * @param first
     * @return
     */
     @Operation(summary = "添加First",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping
     public Result add(@RequestBody  @Parameter(name = "First对象",required = true) First first){
        firstService.save(first);
        return new Result(true,StatusCode.OK.getCode(),"添加成功");
     }

     /***
     * 根据ID查询First数据
     * @param id
     * @return
     */
      @Operation(summary = "根据ID查询First",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
      @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
     @GetMapping("/{id}")
     public Result<First> findById(@PathVariable(value="id") Integer id){
        First first = firstService.getById(id);
        return new Result<First>(true,StatusCode.OK.getCode(),"查询成功",first);
     }

     /***
     * 根据Id批量查询First数据
     * @param ids
     * @return
     */
     @Operation(summary = "根据Id批量查询First数据",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping("/ids")
     public Result<List<First>> findByIds(@RequestBody @Parameter(name = "需要查询的First对象id集合",required = true) List<Integer> ids){
        List<First> firstList = firstService.listByIds(ids);
        return new Result<List<First>>(true,StatusCode.OK.getCode(),"查询成功",firstList);
     }

     /***
     * 查询First全部数据
     * @return
     */
     @Operation(summary = "查询所有First",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @GetMapping
     public Result<List<First>> findAll(){
        List<First> list = firstService.list();
        return new Result<List<First>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
     }


}
